package likou;

import javax.print.attribute.standard.NumberUp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/4/24  10:19
 */
public class _51 {
    static int result = 0;

    public static void main(String[] args) {
        int[] nums = {4,5,6,7};

        List<List<Integer>> lists = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        list.add(nums[0]);
        for (int i = 1; i < nums.length; i++) {
            if (nums[i - 1] < nums[i]) {
                list.add(nums[i]);
            } else {
                lists.add(list);
                list = new ArrayList<>();
                list.add(nums[i]);
            }
        }
        lists.add(list);
        lists.forEach(System.out::println);
        System.out.println("**********************************************************************");
        while (lists.size() != 1) {
            int i = lists.size();
            while (i > 1) {
                lists.add(guibing(lists.remove(0), lists.remove(0)));
                i -= 2;
            }
            if (i == 1) {
                lists.add(lists.remove(0));
            }
        }
        lists.forEach(System.out::println);
        System.out.println(result);
    }

    public static List<Integer> guibing(List<Integer> listF, List<Integer> listL) {
        List<Integer> list = new ArrayList<>();
        while (listF.size() != 0 || listL.size() != 0) {
            if (listF.size() == 0 && listL.size() != 0) {
                list.addAll(listL);
                break;
            }
            if (listF.size() != 0 && listL.size() == 0) {
                list.addAll(listF);
                break;
            }
            if (listF.get(0) < listL.get(0)) {
                list.add(listF.remove(0));
            } else {
                list.add(listL.remove(0));
                result += listF.size();
            }
        }
        return list;
    }
}
